package com.tonysor.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

public class HBaseUtil {
	public static void main(String[] args) {
		// 建立连接
		Configuration conf = HBaseConfiguration.create();
		// conf.set("hbase.rootdir", "hdfs://h1:9000/hbase");

		Connection conn = null;
		Admin admin = null;
		try {
			conn = ConnectionFactory.createConnection(conf);
			admin = conn.getAdmin();
		} catch (IOException e) {
			e.printStackTrace();
		}

		String TABLE_NAME = "test";
		String COLUMN_FAMILY_NAME = "columnfamily";
		String COLUMN_FAMILY_NAME2 = "columnfamily1";
		String[] COLUMNS = { "column0", "column1", "column2" };
		// Some data
		String ROW_KEYS = "row1";
		String[] VALUES = { "value0", "value1", "value2" };
		TableName tableName = null;

		
//		System.out.println("CREATE TABLE =========================="); // 创建表
//		tableName = TableName.valueOf(TABLE_NAME);
//		try { // 存在则删除
//			if (admin.tableExists(tableName)) {
//				admin.disableTable(tableName);
//				admin.deleteTable(tableName);
//			}
//			HTableDescriptor descriptor = new HTableDescriptor(tableName);
//			descriptor.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME.getBytes()))
//					.addFamily(new HColumnDescriptor(COLUMN_FAMILY_NAME2.getBytes()));
//			admin.createTable(descriptor);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
		 

		tableName = TableName.valueOf(TABLE_NAME);
//        System.out.println("PUT VALUE ==========================");
//        // 添加数据
//        try {
//            Table table = conn.getTable(tableName);
//            Put put = new Put(ROW_KEYS.getBytes());
//            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[0].getBytes(), VALUES[0].getBytes());
//            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes(), VALUES[1].getBytes());
//            put.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes(), VALUES[2].getBytes());
//            table.put(put);
//            table.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

//		System.out.println("DELETE VALUE =========================="); // 删除数据： 删除row0的column2列
//		try {
//			Table table = conn.getTable(tableName);
//			Delete delete = new Delete(ROW_KEYS.getBytes());
//			delete.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[2].getBytes());
//			table.delete(delete);
//			table.close();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}

		System.out.println("GET VALUE ==========================");
		// 查询数据
		try {
			Table table = conn.getTable(tableName);
			Get get = new Get(ROW_KEYS.getBytes());
			// get.addFamily(COLUMN_FAMILY_NAME.getBytes()); // 获取特定列族的数据
			// get.addColumn(COLUMN_FAMILY_NAME.getBytes(), COLUMNS[1].getBytes()); //
			// 获取特定列的数据
			Result result = table.get(get);
			// get column family
			result.getFamilyMap(COLUMN_FAMILY_NAME.getBytes())
					.forEach((k, v) -> System.out.println(new String(k) + ":" + new String(v)));
			table.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 关闭连接
		try {
			if (admin != null) {
				admin.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
